<template>
  <div style="flex: 1; width: 100%" id="reportDom">
    <!-- <iframe
      id="iframe"
      :src="iframeSrc"
      style="height: 100%"
      width="100%"
      frameborder="0"
    ></iframe> -->
  </div>
</template>

<script>
export default {
  name: "report",
  data() {
    return {
      iframeSrc: "https://localhost:44346/",
      loading: false,
      onLoadNum: 0,
      routerName: "report",
      reportUrl: {
        灌包车间设备生产状态: {
          param1: "ctl00$ContentPlaceHolder1$TreeView1",
          param2:
            "s诺斯贝尔MES报表\\灌装区报表\\/NBCReport/ProSegmentPackingRecord",
        },
        车间生产排单: {
          param1: "ctl00$ContentPlaceHolder1$TreeView1",
          param2:
            "s诺斯贝尔MES报表\\灌装区报表\\/NBCReport/GuanBaoWorkshopScheduling",
        },
      },
    };
  },
  methods: {
    onLoad() {
      var reportDom = document.getElementById("reportDom");
      var iframe = document.createElement("iframe");
      iframe.style.height = "100%";
      iframe.width = "100%";
      iframe.id = "iframe";
      iframe.src =
        process.env.NODE_ENV === "development"
          ? "https://localhost:44346/"
          : "http://10.1.8.33:9001/";
      reportDom.appendChild(iframe);
      let that = this;
      //监听iframe是否加载完成
      let iframeLoad = function (e) {
        that.onLoadNum += 1;

        if (that.onLoadNum < 2) {
          that.toPath(that.$router.currentRoute.params.param1, true);
        }
      };
      if (!iframe.addEventListener) {
        iframe.attachEvent("onload", iframeLoad);
      }
      iframe.addEventListener("load", iframeLoad, true);
      this.loading = false;
    },
    toPath(param1, isWatch) {
      //reportDom
      var dom = window.frames["iframe"].contentWindow;
      if (this.onLoadNum < 2) {
        let obj = {
          param1: this.reportUrl[param1].param1,
          param2: this.reportUrl[param1].param2,
        };
        setTimeout(() => {
          dom.postMessage(
            obj,
            process.env.NODE_ENV === "development"
              ? "https://localhost:44346/"
              : "http://10.1.8.33:9001/"
          );
        }, 500);

        this.loading = false;
      }
    },
  },
  created() {
    this.reportUrl = JSON.parse(localStorage.getItem("reportObj"));
  },
  mounted() {
    window.addEventListener("hashchange", function (event) {
      console.log(event);
    });
    console.log(this.reportUrl);
    this.loading = true;
    if (this.onLoadNum == 0) {
      this.onLoad();
    }
    if (this.$router.currentRoute.params && this.onLoadNum < 2) {
      var param = this.$router.currentRoute.params;
      this.toPath(param.param1, false);
    }
  },
  watch: {
    $route(to, form) {
      if (to.name == "report") {
        this.onLoadNum = 1;
        this.toPath(to.params.param1, true);
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
